package fuentes;

import java.util.Vector;

public class TablaConversiones {

	private Conversion[][] suma;
	private Conversion[][] resta;
	private Conversion[][] multiplicacion;
	private Conversion[][] division;
	
	private TablaConversiones(){
		suma = new Conversion[3][3];
		resta = new Conversion[3][3];
		multiplicacion = new Conversion[3][3];
		division = new Conversion[3][3];
		
		suma[0][0] = new Conversion("ulong",null,null);
		suma[0][1] = new Conversion("float","ultof",null);
		suma[0][2] = new Conversion("error",null,null);
		suma[1][0] = new Conversion("float",null,"ultof");
		suma[1][1] = new Conversion("float",null,null);
		suma[1][2] = new Conversion("error",null,null);
		suma[2][0] = new Conversion("error",null,null);
		suma[2][1] = new Conversion("error",null,null);
		suma[2][2] = new Conversion("error",null,null);
		
		resta[0][0] = new Conversion("ulong",null,null);
		resta[0][1] = new Conversion("float","ultof",null);
		resta[0][2] = new Conversion("error",null,null);
		resta[1][0] = new Conversion("float",null,"ultof");
		resta[1][1] = new Conversion("float",null,null);
		resta[1][2] = new Conversion("error",null,null);
		resta[2][0] = new Conversion("error",null,null);
		resta[2][1] = new Conversion("error",null,null);
		resta[2][2] = new Conversion("error",null,null);
		
		multiplicacion[0][0] = new Conversion("ulong",null,null);
		multiplicacion[0][1] = new Conversion("float","ultof",null);
		multiplicacion[0][2] = new Conversion("error",null,null);
		multiplicacion[1][0] = new Conversion("float",null,"ultof");
		multiplicacion[1][1] = new Conversion("float",null,null);
		multiplicacion[1][2] = new Conversion("error",null,null);
		multiplicacion[2][0] = new Conversion("error",null,null);
		multiplicacion[2][1] = new Conversion("error",null,null);
		multiplicacion[2][2] = new Conversion("error",null,null);
		
		division[0][0] = new Conversion("float","ultof","ultof");
		division[0][1] = new Conversion("float","ultof",null);
		division[0][2] = new Conversion("error",null,null);
		division[1][0] = new Conversion("float",null,"ultof");
		division[1][1] = new Conversion("float",null,null);
		division[1][2] = new Conversion("error",null,null);
		division[2][0] = new Conversion("error",null,null);
		division[2][1] = new Conversion("error",null,null);
		division[2][2] = new Conversion("error",null,null);
	}
	
	private static TablaConversiones tabla=null;
	
	public static TablaConversiones getInstance(){
		if (tabla==null)
			tabla=new TablaConversiones();
		return tabla;
	}
	
	private int getIndice(String tipo){
		if (tipo.equals("float") || tipo.equals("FLOAT"))
			return 1;
		else 
		if (tipo.equals("ulong") || tipo.equals("ULONG"))
			return 0;
		else return 2;

	}
	
	private Conversion[][] getTablaConversion(String operador){
		if (operador.equals("+"))
			return suma;
		else
		if (operador.equals("-"))
			return resta;
		else
		if (operador.equals("*"))
			return multiplicacion;
		else
		if (operador.equals("/"))
			return division;
		return null;
	}
	
	public String obtenerTipoResultado(String operador, String op1, String op2){
		Conversion[][] tConv = getTablaConversion(operador);
		return (tConv[getIndice(op1)][getIndice(op2)].getTipoResultado());
	}
	public Vector<String> obtenerConversion(String operador, String op1, String op2){
		Vector<String> vector = new Vector<String>();
		Conversion[][] tConv = getTablaConversion(operador);
		
		if (tConv[getIndice(op1)][getIndice(op2)].getTipoOp2()!=null)
			vector.addElement("op2");
		
		if (tConv[getIndice(op1)][getIndice(op2)].getTipoOp1()!=null)
			vector.addElement("op1");
		
		return vector;
	}
}
